#ifndef FITSVIEW_H
#define FITSVIEW_H

#include <QGLWidget>
#include <QTimer>
#include <CCfits>
using namespace CCfits;
// undeclared function types
typedef void(*_glActiveTexture)(GLenum);
typedef void(*_glMultiTexCoord2f)(GLenum, GLfloat, GLfloat);
typedef void(*_glMultiTexCoord1f)(GLenum, GLfloat);


class FitsView : public QGLWidget {
	Q_OBJECT
	// fits object stuff
	FITS *fits;
	unsigned long imWidth, imHeight;
	valarray<unsigned long> imContents; // vector like container, 16 bit
	unsigned short *imData; // [imWidth*imHeight]
	GLuint textures[2]; // texture list

	// view stuff
	double midValue, grayScale; // value of 50% gray [0,1], d gray/ d value [-infty, infty]
	double zoom; // 2 = linear 2 texels per pixel, -2 = linear twice the size of the widget
	double currentAngle;
	bool suspendDraw;

	// miscellaneous
	bool glInited; // does same as context()->initialized() which is protected
	void loadTexture(); // initializeGL/setFits
	// smooth rotation
	QTimer* rotationTimer;
	double beginAngle, endAngle; int iteration;
	// undeclared functions
	_glActiveTexture glActiveTexture;
	_glMultiTexCoord2f glMultiTexCoord2f;
	_glMultiTexCoord1f glMultiTexCoord1f;
public:
	FitsView(QWidget *parent);
	~FitsView();
	void setAzimuth(double a);
	void setSuspendDraw(bool suspend = true);
	double getRotation();
	QSize sizeHint() const; /// implementation of QWidget
	void setGray(double midValue = .5, double grayScale = 1);
protected:
	virtual void initializeGL();
	virtual void resizeGL(int w, int h);
	virtual void paintGL();
	virtual QSize minimumSizeHint() const;
	virtual int heightForWidth(int width) const;
public slots:
	void setFits(FITS *fits);
	void setColormap(const unsigned *map);
	void setRotation(double angle);
	void rotationTimerCallback(); // must be slot for timer to connect to
};

#endif // FITSVIEW_H
